#coding: utf8

__access__ = 0

print """<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>NStats</TITLE>
<style type="text/css">
<!--
body { background-attachment : fixed; font-family: "Verdana", Arial, sans-serif; color: #FFFFFF; font-size: 12px; text-align: left }
h1 { font-family: "Arial"; color: #FFFFFF; font-size: 26px; font-style: italic; font-weight: normal; margin: 4px }
h3 { font-family: "Arial"; color: #FFFFFF; font-size: 20px; font-style: italic; font-weight: normal; margin: 3px }
h4 { font-family: "Arial"; color: #00F400; font-size: 16px; font-style: italic; font-weight: normal; margin: 2px }
h5 { font-family: "Arial"; color: #FFFFFF; font-size: 16px; font-style: italic; font-weight: normal; margin: 2px }
.txt1 { font-family: "Verdana"; color: #FFFFFF; font-size: 12px; text-align: left }
.txt2 { font-family: "Verdana"; color: #00F400; font-size: 12px; text-align: left }

.t1 { font-family: "Verdana"; color: #080000; font-size: 12px; text-align: left; font-weight: bold; font-style: italic }
.t2 { font-family: "Verdana"; color: #080000; font-size: 12px; text-align: left }
.t3 { font-family: "Verdana"; color: #080000; font-size: 12px; text-align: right }
.t4 { font-family: "Verdana"; color: #080000; font-size: 12px; text-align: center }
.t5 { font-family:"Arial"; color:#080000; font-size:12px; text-align:left; font-weight:normal; font-style: italic }
.t6 { font-family:"Verdana"; color:#080000; font-size:10px; text-align:right; font-style: normal }

.s1 { font-family: "Arial Narrow"; color: #00F400; font-size: 9px; vertical-align: bottom; text-align: center  }
.s2 { font-family: "Arial Narrow"; color: #FFFFFF; font-size: 10px; vertical-align: bottom; text-align: center  }
.s3 { font-family: "Arial Narrow"; color: #080000; font-size: 8px; vertical-align: bottom; text-align: center; background-color: #C090A8 }
.s4 { font-family: "Verdana"; color: #080000; font-size: 10px; text-align: left }
.s5 { font-family: "Verdana"; color: #080000; font-size: 10px; text-align: left }
.toc { font-family: "Arial"; color: #00F400; font-size: 16px; font-style: italic; font-weight: normal; margin: 2px }
.ra { font-family: "Arial"; color: #080000; font-size: 32px; text-align: center; border-style: outset; border-width: 4px }
.rn { font-family:"Verdana"; color:#FFFFFF;font-size:16px; text-align:left;font-weight:bold; font-style:normal }
.ad { border-style: outset; border-width: 1px; font-family: "Verdana", Arial, sans-serif; color: #080000; font-size: 10px; vertical-align: middle; text-align: center }
.am { vertical-align: top }
.cr { border-style: outset; border-width: 2px; font-family: Arial, sans-serif; color: #FFFFFF; font-size: 12px; vertical-align: middle; text-align: left }
.v1 { background-color: #A07088 }
.v2 { background-color: #70A098 }
.v3 { background-color: #7080B8 }
.v4 { background-color: #C090A8 }
.v5 { background-color: #90C0B8 }
.v6 { background-color: #90A0D8 }
.v7 { background-color: #70A098 }
.v8 { background-color: #003E5E }
-->
</style>
</HEAD>
<body topmargin="0" leftmargin="0" background ="blue_1_2.gif" bgcolor="#003E5E" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">"""

print '<p align="right">'

import time
t = time.time()
print time.strftime('%Y-%m-%d %H:%M:%S, статистика за прошедшие ')



from functions import mysql_db

db = mysql_db()
dc = db.query('SELECT count(distinct date) from trafficlog').fetchone()[0]
print dc
print ' дней </p>'

print '<h2 align="center">Статистика использования канала 192.168.0.157</h2>'

for j in range(2):
  i = 1 + j
  print '<p align="left"><h3 align="center">'
  print ('Интернет траффик', 'Локальный траффик')[j]
  print '<table align="center"><tr>'
  #########################
  print '<td valign="bottom">'
  print '<h4 align="center">'
  print 'Распределение по часам'
  print '</h4>'
  print '</h3></p><p align="center">'
  print '<table border=0 align="center">'
  print '<tr class=s1 valign=bottom>'
  q = db.query('SELECT hour, sum(bytes_in+bytes_out) from trafficlog '+\
  'where traffic_type=%s group by hour order by hour', (str(i), )).fetchall()
  for k, v in q:
   desc = "%d мегабайт" % int(v/dc/1000000)
   print '<td><img src="pipe1v.gif" width="12" height="%s" alt="%s" title="%s"></td>' % \
            (int(v/1000000/dc/(9*j+1)), desc, desc)
  print '</tr>'
  print '<tr class=s3>'
  for k, v in q:
   print '<td>%s</td>' % (k, )
  print '</tr></table>'
  print '</td>'
  #########################################
  print '<td valign="bottom">'
  print '<h4 align="center">'
  print 'Распределение по дням недели'
  print '</h4>'
  print '</h3></p><p align="center">'
  print '<table border=0 align="center">'
  print '<tr class=s1 valign=bottom>'
  q = db.query('SELECT DAYOFWEEK(date) as day, sum(bytes_in+bytes_out) from trafficlog '+\
  'where traffic_type=%s group by day order by day', (str(i), )).fetchall()
  for k, v in q:
   desc = '%0.1f гигабайт' % float(7*v/dc/1000000000)
   print '<td><img src="pipe1v.gif" width="12" height="%s" alt="%s" title="%s"></td>' % \
            (int(7*v/24000000/dc/(9*j+1)), desc, desc)
  print '</tr>'
  print '<tr class=s3>'
  for k, v in q:
   print '<td>%s</td>' % ["Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"][k-1]
  print '</tr></table>'
  print '</td>'
  ###########################
  print '<td valign="bottom">'
  print '<h4 align="center">'
  print 'Распределение по дням'
  print '</h4>'
  q = db.query('SELECT date, sum(bytes_in+bytes_out) from trafficlog where traffic_type=%s group by date order by date', (str(i), )).fetchall()
  print '<Table border=0 cellspacing=1 cellpadding=0 align="center"><tr class=s1 valign=bottom>'
  for k, v in q:
   print '<td><img src="pipe1v.gif" width="6" height="%s" alt="%s" title="%s"/></td>' % \
   (int(v/24000000/(9*j+1)), '%s: %s мегабайт' % (k, int(v/1000000)), '%s: %s мегабайт' % (k, int(v/1000000)))
  print '</tr></table>'
  print '</td>'
  ######################################
  print '</tr></table>'
  print '</p>'
  print '<hr/>'


db.close()


print '<p align="center" id="utyp"><b></b></p><p align="right">Сгенерировано за %d мс.</p>' % int(1000*(time.time()-t))

print "</BODY></HTML>"
